package com.jp.knowledge.my.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.activity.BaseFragment;
import com.jp.knowledge.my.adapter.MyReeceAdapter;
import com.jp.knowledge.my.business.MyReeceBusiness;
import com.jp.knowledge.my.model.MyReeceModel;
import com.jp.knowledge.view.RecycleViewDiver;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;

/**
 * 我的红包 发出的/收到的
 *
 * @author gzejia 978862664@qq.com
 */
@ContentView(R.layout.fragment_my_reece)
public class MyReeceFragment extends BaseFragment {

    @ViewInject(R.id.reece_rv)
    private RecyclerView mReeceRv;

    @ViewInject(R.id.reece_lly_hind)
    private View mHindLly;

    @ViewInject(R.id.reece_tv_hind)
    private TextView mHindTv;

    public static final int REECE_GET = 0;
    public static final int REECE_SEND = 1;

    private int mReeceType = REECE_GET;
    private MyReeceBusiness mBusiness = new MyReeceBusiness();
    private MyReeceAdapter mReeceAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return x.view().inject(this, inflater, container);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initReeceRv();
        initOthers();
    }

    private void initReeceRv() {
        mReeceRv.setHasFixedSize(true);
        mReeceRv.addItemDecoration(new RecycleViewDiver(getActivity(), LinearLayout.VERTICAL));
        mReeceRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        mReeceAdapter = new MyReeceAdapter(getActivity(), mReeceType == REECE_SEND ?
                mBusiness.getReeceData() : new ArrayList<MyReeceModel>());
        mReeceRv.setAdapter(mReeceAdapter);
    }

    private void initOthers() {
        if (mReeceType == REECE_GET) {
            mHindTv.setText(getString(R.string.my_reece_tv_get_hind));
        } else if (mReeceType == REECE_SEND) {
            mHindTv.setText(getString(R.string.my_reece_tv_send_hind));
        }
        mHindLly.setVisibility(null == mReeceAdapter || mReeceAdapter.mList.isEmpty() ?
                View.VISIBLE : View.GONE);
    }

    /**
     * @param type {@link #REECE_SEND} 发出的红包；{@link #REECE_GET} 收到的红包；
     * @see #mReeceType
     */
    public void setReeceType(int type) {
        this.mReeceType = type;
    }
}
